sceneHillClimb.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { _decorator, Camera, Component, director, EPhysics2DDrawFlags, Node, PhysicsSystem2D } from 'cc';
  2. import { CheckButton } from './CheckButton';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('sceneHillClimb')
  5. export class sceneHillClimb extends Component {
  6. // @property(CheckButton)
  7. // checkButton: CheckButton = null!;
  8. @property(Node)
  9. nodeCamera: Node = null;
  10. @property(Node)
  11. nodeCarBody: Node = null;
  12. // @property(CheckButton)
  13. // checkButton3: CheckButton = null!;
  14. protected onEnable(): void {
  15. console.log(this.nodeCarBody.getWorldPosition());
  16. // this.checkButton.bindFunction((from: CheckButton) => {
  17. // PhysicsSystem2D.instance.debugDrawFlags = from.onoff
  18. // ? EPhysics2DDrawFlags.Shape | EPhysics2DDrawFlags.Joint
  19. // : EPhysics2DDrawFlags.Controller;
  20. // this.scheduleOnce(() => {
  21. // const debugDrawNode = this.node.getChildByName('PHYSICS_2D_DEBUG_DRAW');
  22. // if (debugDrawNode) {
  23. // debugDrawNode.layer = this.nodeCarBody.layer;
  24. // }
  25. // }, 1 / 60);
  26. // });
  27. // this.checkButton3.bindFunction((from: CheckButton) => {
  28. // director.loadScene('scene-sample-curvetexture');
  29. // });
  30. }
  31. start() {}
  32. protected lateUpdate(dt: number): void {
  33. const debugDrawNode = this.node.getChildByName('PHYSICS_2D_DEBUG_DRAW');
  34. if (debugDrawNode) {
  35. debugDrawNode.layer = this.nodeCarBody.layer;
  36. }
  37. }
  38. update(deltaTime: number) {
  39. //让相机跟随车体 ,要注意坐标转换
  40. const carpos = this.nodeCarBody.getWorldPosition();
  41. // console.log('carpos', carpos);
  42. this.nodeCamera.setWorldPosition(carpos.x + 300, carpos.y, this.nodeCamera.getWorldPosition().z);
  43. const currentPos = this.nodeCamera.getWorldPosition();
  44. const targetX = carpos.x + 300;
  45. const lerpFactor = 0.2;
  46. const newX = currentPos.x + (targetX - currentPos.x) * lerpFactor;
  47. const targetY = carpos.y;
  48. const newY = currentPos.y + (targetY - currentPos.y) * lerpFactor;
  49. this.nodeCamera.setWorldPosition(newX, newY, currentPos.z);
  50. }
  51. }